package com.caojin.propertyInjector.propertyResolver;

import java.util.Properties;

import com.caojin.propertyInjector.PropertiesCache;
import com.caojin.propertyInjector.locationResolver.LocationResolver;

/**
 * 配置文件解析器
 * @author caojin
 */
public abstract class PropertiesResolver {
    //路径解析器
	protected LocationResolver locationResolver;
	//是否使用缓存
	protected boolean useCache;
	
	public void setConfiguration(LocationResolver locationResolver,boolean useCache) {
			this.locationResolver = locationResolver;
			this.useCache=useCache;
	}

	/**
	 * 将配置信息解析为properties
	 * @return
	 */
	public Properties resolveProperties() {
		if (useCache) {
			Properties props = getPropertiesFormCache();
			if (props == null) {
				props = getPropertiesFromLocation();
				PropertiesCache.putProperties(locationResolver.getFileUrl(),
						props);
			}
			return props;
		} else {
			return getPropertiesFromLocation();
		}
	}

	//从缓存获取已解析的propeties
	protected Properties getPropertiesFormCache() {
		String fileUrl = locationResolver.getFileUrl();
		return PropertiesCache.getProperties(fileUrl);
	}

	//从对应位置获取properties
	protected abstract Properties getPropertiesFromLocation();
}
